package com.rst.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.Attendance;
import com.rst.model.Section;
import com.rst.model.Student;
import com.rst.service.AttendanceService;
import com.rst.service.StudentService;

@Controller
public class AttendanceController {

	@Autowired
	AttendanceService attendanceService;
	
	@Autowired
	StudentService studentService ;

	
	@RequestMapping(value="/attendanceType", method=RequestMethod.GET)
	public ModelAndView attendanceType()
	{
		ModelAndView mav = new ModelAndView("attendance/attendanceType");
		return mav;
	}

	@RequestMapping(value="/employeeAttendance", method=RequestMethod.GET)
	public ModelAndView employeeAttendance()
	{
		ModelAndView mav = new ModelAndView("attendance/employeeAttendanceRegister");
		return mav;
	}

	@RequestMapping(value="/studentAttendance", method=RequestMethod.GET)
	public ModelAndView studentAttendance()
	{
		ModelAndView mav = new ModelAndView("attendance/studentAttendanceRegister");		
		List<Section> section = studentService.getClassAndSections();
		mav.addObject("sections", section);		
		return mav;
	}
	
	  @RequestMapping(value = "/getStudents/{sectionId}", method = RequestMethod.GET)	  
	  public @ResponseBody List<Student> getStudents(@PathVariable("sectionId") Long sectionId) {
		  return studentService.getStudentsBySectionId(sectionId);
	 }
	
		@RequestMapping(value="/saveAttendance", method=RequestMethod.POST)
		public @ResponseBody String saveAttendance(@ModelAttribute("attendance")Attendance attendance, SessionStatus status)
		{
			Long id = attendanceService.saveAttendance(attendance);
			status.setComplete();
			return "Success";
		}
		
		@RequestMapping(value="/editAttendance", method=RequestMethod.GET)
		public @ResponseBody Attendance editAttendance(@PathVariable("attendanceId")Long attendanceId)
		{
			Attendance attendance = attendanceService.getAttendanceById(attendanceId); 
			return attendance;
		}

		@RequestMapping(value="/ajax/getMonthCalendar", method=RequestMethod.GET)
		public @ResponseBody String getMonthCalendar(@RequestParam("month")int month , @RequestParam("year")int year ,  @RequestParam("sectionId")Long sectionId)
		{
			try {
				String monthStr = attendanceService.getCalendarByMonth(month , year, sectionId);
				return monthStr;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "Error";
		}

}
